package priv.stones.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.stereotype.Service;
import priv.stones.controller.administrate.shiro.ShiroUtils;
import priv.stones.entity.SysAdmin;
import priv.stones.entity.SysMenu;
import priv.stones.mapper.SysAdminDao;
import priv.stones.service.SysAdminService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Stones
 * @Description:
 * @Date: 2017-10-11
 */
@Service
public class SysAdminServiceImpl extends ServiceImpl<SysAdminDao, SysAdmin> implements SysAdminService {

    @Override
    public boolean updatePwd(String adminID, String newPwd) {
        SysAdmin updateField = new SysAdmin();
        updateField.setId(adminID);
        updateField.setLoginsalt(RandomStringUtils.randomAlphanumeric(20));
        updateField.setLoginpwd(ShiroUtils.sha256(newPwd, updateField.getLoginsalt()));
        return updateById(updateField);
    }

    @Override
    public List<SysMenu> selectOwnMenus(String adminID) {
        return baseMapper.selectMenus(adminID);
    }

    @Override
    public int relateRoles(String[] roles, String adminID) {
        baseMapper.deleteRoles(adminID);
        if (roles != null) {
            Map<String, String> map = new HashMap<>();
            for (String role : roles) {
                map.put(role, adminID);
            }
            baseMapper.insertRoles(map);
            return roles.length;
        }
        return 0;
    }

    @Override
    public List<String> selectRoleIdsById(String adminID) {
        return baseMapper.selectRoleIds(adminID);
    }


}
